package com.fsnbigdata.domain;

import com.fsnbigdata.util.ChineseInital;
import io.springlets.format.EntityFormat;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Version;
import javax.validation.Valid;
import javax.validation.constraints.*;

import org.hibernate.annotations.Index;
import org.hibernate.validator.constraints.Length;
import org.springframework.roo.addon.javabean.annotations.RooEquals;
import org.springframework.roo.addon.javabean.annotations.RooJavaBean;
import org.springframework.roo.addon.javabean.annotations.RooToString;
import org.springframework.roo.addon.jpa.annotations.entity.JpaRelationType;
import org.springframework.roo.addon.jpa.annotations.entity.RooJpaEntity;
import org.springframework.roo.addon.jpa.annotations.entity.RooJpaRelation;
import org.springframework.util.Assert;

/**
 * = Product
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJavaBean
@RooToString
@RooJpaEntity(entityFormatExpression = "#{name}")
@RooEquals(isJpaEntity = true)
@Entity
@EntityFormat("#{name}")
public class Product {

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @Version
    private Integer version;

    /**
     * 名称
     */
    @NotNull
    @Length(min=1,max=255,message="产品名称长度必须在1-255之间")
    private String name;

    /**
     * 拼音首字母
     */
    @Index(name="Product_initial")
    private String initial;

    /**
     * 产品条码
     */
    @Length(min=0,max=255,message="产品条码长度必须在0-255之间")
    private String barcode;

    /**
     * 品牌
     */
    @Length(min=0,max=255,message="产品品牌长度必须在0-255之间")
    private String brand;

    /**
     * 产品分类
     */
    @Length(min=0,max=255,message="产品分类长度必须在0-255之间")
    private String category;

    /**
     * 配料表
     */
    @Length(min=0,max=255,message="产品配料长度必须在0-255之间")
    private String ingredients;

    /**
     * 适宜人群
     */
    @Length(min=0,max=255,message="产品适宜人群长度必须在0-255之间")
    private String targetGroup;

    /**
     * 营养成分
     */
    @Length(min=0,max=255,message="产品营养成分长度必须在0-255之间")
    private String nutrition;

    /**
     * 特色
     */
    @Length(min=0,max=255,message="产品特色长度必须在0-255之间")
    private String feature;

    /**
     * 产品简介
     */
    @Lob
    @Basic(fetch = FetchType.LAZY)
    private String description;

    /**
     * 规格型号
     */
    @Length(min=0,max=255,message="规格型号长度必须在0-255之间")
    private String specification;

    /**
     * 是否出口
     */
    @Length(min=0,max=255,message="规格是否出口长度必须在0-255之间")
    private String isExport;

    /**
     * 创建者
     */
    @Length(min=0,max=255,message="规格创建者长度必须在0-255之间")
    private String creator;
    
    /**
     * 企业库：1 食品企业，2 药品企业，3 其他生物企业
     */
    @Min(value = 0,message = "产品类型最小值必须为0")
    @Max(value = 4,message = "产品类型最大值必须为3")
    private Integer type;
    
    public Integer getType() {
        return type;
    }
    
    public void setType(Integer type) {
        this.type = type;
    }

    /**
     * 相关生物信息
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "product", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    @Valid
    private Set<ProductOrganism> organisms = new HashSet<ProductOrganism>();

    /**
     * 相关企业信息
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "product", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    @Valid
    private Set<ProductEnterprise> enterprises = new HashSet<ProductEnterprise>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "product", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    @Valid
    private Set<ProductImage> images = new HashSet<ProductImage>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE = "The given Iterable of items to add can't be null!";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE = "The given Iterable of items to add can't be null!";

    /**
     * This `equals` implementation is specific for JPA entities and uses
     * the entity identifier for it, following the article in
     * https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
     *
     * @param obj
     * @return Boolean
     */
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        // instanceof is false if the instance is null
        if (!(obj instanceof Product)) {
            return false;
        }
        return getId() != null && Objects.equals(getId(), ((Product) obj).getId());
    }

    /**
     * This `hashCode` implementation is specific for JPA entities and uses a fixed `int` value to be able
     * to identify the entity in collections after a new id is assigned to the entity, following the article in
     * https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
     *
     * @return Integer
     */
    @Override
    public int hashCode() {
        return 31;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Long
     */
    public Long getId() {
        return this.id;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Integer
     */
    public Integer getVersion() {
        return this.version;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param version
     */
    public void setVersion(Integer version) {
        this.version = version;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getName() {
        return this.name;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getInitial() {
        return this.initial;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param initial
     */
    public void setInitial(String initial) {
        this.initial = initial;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getBarcode() {
        return this.barcode;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param barcode
     */
    public void setBarcode(String barcode) {
        this.barcode = barcode;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getBrand() {
        return this.brand;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param brand
     */
    public void setBrand(String brand) {
        this.brand = brand;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getCategory() {
        return this.category;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param category
     */
    public void setCategory(String category) {
        this.category = category;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getIngredients() {
        return this.ingredients;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ingredients
     */
    public void setIngredients(String ingredients) {
        this.ingredients = ingredients;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getTargetGroup() {
        return this.targetGroup;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param targetGroup
     */
    public void setTargetGroup(String targetGroup) {
        this.targetGroup = targetGroup;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getNutrition() {
        return this.nutrition;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param nutrition
     */
    public void setNutrition(String nutrition) {
        this.nutrition = nutrition;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getFeature() {
        return this.feature;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param feature
     */
    public void setFeature(String feature) {
        this.feature = feature;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getDescription() {
        return this.description;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param description
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getSpecification() {
        return this.specification;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param specification
     */
    public void setSpecification(String specification) {
        this.specification = specification;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getIsExport() {
        return this.isExport;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param isExport
     */
    public void setIsExport(String isExport) {
        this.isExport = isExport;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getCreator() {
        return this.creator;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param creator
     */
    public void setCreator(String creator) {
        this.creator = creator;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<ProductOrganism> getOrganisms() {
        return this.organisms;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param organisms
     */
    public void setOrganisms(Set<ProductOrganism> organisms) {
        this.organisms = organisms;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<ProductEnterprise> getEnterprises() {
        return this.enterprises;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param enterprises
     */
    public void setEnterprises(Set<ProductEnterprise> enterprises) {
        this.enterprises = enterprises;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<ProductImage> getImages() {
        return this.images;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param images
     */
    public void setImages(Set<ProductImage> images) {
        this.images = images;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String toString() {
        return "Product {" + "id='" + id + '\'' + ", version='" + version + '\'' + ", name='" + name + '\'' + ", initial='" + initial + '\'' + ", barcode='" + barcode + '\'' + ", brand='" + brand + '\'' + ", category='" + category + '\'' + ", ingredients='" + ingredients + '\'' + ", targetGroup='" + targetGroup + '\'' + ", nutrition='" + nutrition + '\'' + ", feature='" + feature + '\'' + ", description='" + description + '\'' + ", specification='" + specification + '\'' + ", isExport='" + isExport + '\'' + ", creator='" + creator + '\'' + ", ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE='" + ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE + '\'' + ", ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE='" + ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE + '\'' + "}" + super.toString();
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param organismsToAdd
     */
    public void addToOrganisms(Iterable<ProductOrganism> organismsToAdd) {
        Assert.notNull(organismsToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (ProductOrganism item : organismsToAdd) {
            this.organisms.add(item);
            item.setProduct(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param organismsToRemove
     */
    public void removeFromOrganisms(Iterable<ProductOrganism> organismsToRemove) {
        Assert.notNull(organismsToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (ProductOrganism item : organismsToRemove) {
            this.organisms.remove(item);
            item.setProduct(null);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param enterprisesToAdd
     */
    public void addToEnterprises(Iterable<ProductEnterprise> enterprisesToAdd) {
        Assert.notNull(enterprisesToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (ProductEnterprise item : enterprisesToAdd) {
            this.enterprises.add(item);
            item.setProduct(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param enterprisesToRemove
     */
    public void removeFromEnterprises(Iterable<ProductEnterprise> enterprisesToRemove) {
        Assert.notNull(enterprisesToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (ProductEnterprise item : enterprisesToRemove) {
            this.enterprises.remove(item);
            item.setProduct(null);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param imagesToAdd
     */
    public void addToImages(Iterable<ProductImage> imagesToAdd) {
        Assert.notNull(imagesToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (ProductImage item : imagesToAdd) {
            this.images.add(item);
            item.setProduct(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param imagesToRemove
     */
    public void removeFromImages(Iterable<ProductImage> imagesToRemove) {
        Assert.notNull(imagesToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (ProductImage item : imagesToRemove) {
            this.images.remove(item);
            item.setProduct(null);
        }
    }

    @PrePersist
    public void prePersist(){
        this.setInitial(ChineseInital.getFirstLetter(this.getName()));
    }

    @PreUpdate
    public void preUpdate(){
        this.setInitial(ChineseInital.getFirstLetter(this.getName()));
    }
}
